#!/bin/bash
# txttable format:
#配置SSH公钥 0:00   5
#Fork仓库    2:47   20
#git克隆仓库到本地   4:46   20
#用Greenfoot打开工程 8:10   20
# 注意不要出现"08:10"，脚本解析这种数据会出错
#set -x
if [ $# != 2 ]; then
    echo "Usage: drawtxt.sh txttable inputfile"
    exit 1
fi

txttable=$1
inputfile=$2
suffix=${inputfile##*.}
filename=${inputfile%.*}
fontfile='/usr/share/fonts/truetype/winfonts/msyhbd.ttc'

[ ! -d "tmp" ] && mkdir ./tmp
prefix="video"
counter=1
oldfile=$inputfile
while read content stime duration; do
    echo "read txt table, content:${content} stime:${stime}, duration:${duration}"
    printf -v num "%02d" $counter
    newfile="./tmp/${prefix}${num}.${suffix}"
    #stime转换成秒
    readarray -d: -t arr <<< "${stime}"
    start=$[${arr[0]} * 60 + ${arr[1]}]
    finish=$[${start} + ${duration}]
    echo "start: ${start}"
    ffmpeg -nostdin -i ${oldfile} -vf "drawtext=fontsize=40:fontfile=${fontfile}:text=${content}:fontcolor=blue:x=300:y=300:line_spacing=10:enable='between(t,${start},${finish})'" ${newfile}
    counter=$[$counter+1]
    oldfile=$newfile
done < $txttable

cp ${oldfile} ./${filename}_txt.${suffix}
[ -d "tmp" ] && rm -fr ./tmp
#set +x
